package cn.ibizlab.util.domain;

import cn.hutool.core.bean.DynaBean;
import cn.ibizlab.util.helper.BeanCache;
import cn.ibizlab.util.helper.CachedBeanCopier;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.data.annotation.Transient;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Data
public class DTOBase implements IEntity,Serializable {


    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    private Set<String> focusNull;


    public void modify(String field,Object val) {
        if(val==null) {
            this.getFocusNull(true).add(field.toLowerCase());
        }
        else {
            this.getFocusNull(true).remove(field.toLowerCase());
        }
    }

    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    @Override
    public Set<String> getFocusNull() {
        if(focusNull==null) {
            focusNull=new HashSet<>();
        }
        if(!focusNull.isEmpty() && extensionparams.containsKey("dirtyflagenable"))
        {
            Set<String> nocheck=new HashSet<>();
            for(String key:focusNull)
            {
                if(!extensionparams.containsKey(key+"dirtyflag")) {
                    nocheck.add(key);
                }
            }
            for(String key:nocheck) {
                focusNull.remove(key);
            }
        }
        return focusNull;
    }

    private Set<String> getFocusNull(boolean newflag) {
        if(newflag && focusNull==null) {
            focusNull=new HashSet<>();
        }
        return focusNull;
    }

    @Override
    public boolean isFocusNull(String field){
        return getFocusNull().contains(field.toLowerCase());
    }

    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    private Map<String,Object> extensionparams=new HashMap<>();

    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    @Override
    public Map<String, Object> getExtensionparams() {
        return extensionparams;
    }

    public void setExtensionparams(Map<String, Object> extensionparams) {
        this.extensionparams = extensionparams;
    }

    @JsonAnyGetter
    @JSONField(name = "_any", unwrapped = true, serialize = true, deserialize = false)
    public Map<String , Object> any() {
        return extensionparams;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    private DynaBean map;

    @JsonIgnore
    @JSONField(serialize = false)
    @Transient
    private DynaBean getMap()
    {
        if(map==null) {
            map=DynaBean.create(this);
        }
        return  map;
    }

    public Object get(String field) {
        String fieldRealName= BeanCache.getFieldRealName(this.getClass(),field);
        if(!ObjectUtils.isEmpty(fieldRealName)) {
            return getMap().get(fieldRealName);
        }
        else {
            return this.extensionparams.get(field.toLowerCase());
        }
    }

    @JsonAnySetter
    @JSONField(name = "_any", unwrapped = true, serialize = false, deserialize = true)
    public void set(String field, Object value) {
        field=field.toLowerCase();
        String fieldRealName=BeanCache.getFieldRealName(this.getClass(),field);
        if(!ObjectUtils.isEmpty(fieldRealName)) {
            if (value == null) {
                getMap().set(fieldRealName, null);
            }
            else {
                getMap().set(fieldRealName, BeanCache.fieldValueOf(this.getClass(), fieldRealName, value));
            }
        }
        else {
            this.extensionparams.put(field.toLowerCase(),value);
        }
    }

    /**
     * 复制当前对象数据到目标对象
     * @param targetDTO 目标数据对象
     * @param bIncEmpty  是否包括空值
     * @param <T>
     * @return
     */
    public <T> T copyTo(T targetDTO, boolean bIncEmpty){
        CachedBeanCopier.copy(this,targetDTO,bIncEmpty);
        return targetDTO;
    }

}

